package com.remote.common.context;

import com.remote.common.RemoteHandlerRegister;
import com.remote.common.context.serialization.ProtobufDecoder;
import com.remote.common.context.serialization.ProtobufEncoder;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;

/**
 * t通道处理器
 */
@ChannelHandler.Sharable
public class RemoteChannelInitializer extends ChannelInitializer<SocketChannel> implements RemoteHandlerRegister {
    /**
     *
     */
    private Class<?> decoderClass;
    private Class<?> encoderClass;
    private ChannelHandler channelHandler;


    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        ChannelPipeline pipeline = socketChannel.pipeline();
        pipeline.addLast(new ProtobufDecoder(this.decoderClass));
        pipeline.addLast(new ProtobufEncoder(this.encoderClass));
        pipeline.addLast(this.channelHandler);
    }

    @Override
    public void setDecoderClass(Class<?> decoderClass) {
        this.decoderClass = decoderClass;
    }

    @Override
    public void setEncoderClass(Class<?> encoderClass) {
        this.encoderClass = encoderClass;
    }

    @Override
    public void setChannelHandler(ChannelHandler channelHandler) {
        this.channelHandler = channelHandler;
    }
}